package com.jhoncy.rapid.base.bean;

import com.jhoncy.rapid.base.pojo.BaseUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.util.LinkedHashMap;

/**
 * @author zhuangqi
 * @date 2017/7/26.
 */
public class UserInfo {

    public static BaseUser getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LinkedHashMap<String,Object> map = (LinkedHashMap<String, Object>) ((OAuth2Authentication) authentication).getUserAuthentication().getDetails();
        BaseUser user = (BaseUser) map.get("principal");
        return user;
    }

    public static String getUserId() {
        return getUser().getUserId();
    }
}
